package com.mt.controller;


import com.mt.constants.Result;
import com.mt.dto.MessageLoginRequest;
import com.mt.dto.ModifyPasswordRequest;
import com.mt.pojo.User;
import com.mt.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Wang
 * @Description  用户中心控制
 * @since 2020-11-12
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @GetMapping("/auth")
    public Result AuthUser(@RequestParam("token")String token, @RequestParam("type")String type,
                           @RequestParam("url")String url, @RequestParam("aclCode")String aclCode) {
        log.info(token+" "+type+" "+url+" "+aclCode);
        Result<Object> result = new Result<>();
        String auth = userService.AuthUser(token, type, url, aclCode);
        result.success(auth);
        return result;
    }


    @PostMapping("/modify")
    public Result modifyUser(@RequestBody User user,@RequestParam("userId") String userId) {
        user.setUserId(userId);
        log.info(user.toString());
        Result<Object> result = new Result<>();
        result.success(userService.modifyUser(user));
        return result;
    }

    @PostMapping("/modifyPassword")
    public Result modifyUserPassword(@RequestBody ModifyPasswordRequest request, @RequestParam("userId") String userId) {
        request.setUserId(userId);
        log.info(request.toString());
        Result<Object> result = new Result<>();
        result.success(userService.modifyUserPassword(request));
        return result;
    }
    @PostMapping("/admin/modify")
    public Result modifyUser(@RequestBody User user) {
        log.info(user.toString());
        Result<Object> result = new Result<>();
        result.success(userService.modifyUser(user));
        return result;
    }

    @PostMapping("/admin/add")
    public Result addUser(@RequestBody User user) {
        log.info(user.toString());
        Result<Object> result = new Result<>();
        result.success(userService.addUser(user));
        return result;
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info(user.toString());
        Result<Object> result = new Result<>();
        result.success(userService.login(user));
        return result;
    }


    @PostMapping("/message/login")
    public Result messageLogin(@RequestBody MessageLoginRequest request){
        Result<Object> result = new Result<>();
        result.success(userService.messageLogin(request.getPhone(),request.getCode()));
        return result;
    }
    @PostMapping("/message")
    public Result sendMessage(@RequestBody User user){
        Result<Object> result = new Result<>();
        result.success(userService.sendMessage(user.getPhone()));
        return result;
    }

    @GetMapping
    public Result getUserInfo(@RequestParam("userId") String userId){
        log.info(userId);
        Result<Object> result = new Result<>();
        result.success(userService.getUserInfo(userId));
        return result;
    }
    @GetMapping("/detail")
    public Result getUserDetailInfo(@RequestParam("userId") String userId){
        log.info(userId);
        Result<Object> result = new Result<>();
        result.success(userService.getUserDetailInfo(userId));
        return result;
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        Result<Object> result = new Result<>();
        Boolean addUser = userService.addUser(user);
        if (addUser){
            result.success();
        }
        return  result;
    }


    /********管理后台*******/
    @GetMapping("/list")
    public Result getUserList(Integer currentPage,Integer pageSize,String key){
        Result<Object> result = new Result<>();
        result.success(userService.listUserInfo(currentPage,pageSize,key));
        return result;
    }

    @PostMapping("/delete")
    public Result deleteUser(@RequestBody User user){
        Result<Object> result = new Result<>();
        result.success(userService.deleteUser(user.getUserId()));
        return result;
    }


}
